﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WCell.RealmServer.Commands;
using WCell.RealmServer.Gossips;
using WCell.Intercommunication.DataTypes;
using WCell.Util.Commands;
using WCell.RealmServer.Entities;
using WCell.RealmServer.Guilds;

namespace Kingdoms
{
    class TerritoryManagementGossip : RealmServerCommand
    {
        protected override void Initialize()
        {
            Init("Kingdoms", "kingdoms", "kd", "Kingdom", "kingdom");
            EnglishDescription = "The kingdom management system";
        }

        public override RoleStatus RequiredStatusDefault
        {
            get { return RoleStatus.Player; }
        }

        public override void Process(CmdTrigger<RealmServerCmdArgs> trigger)
        {
            Character chr = ((Character)trigger.Args.Character);

            GossipMenu menu = new GossipMenu();
            if(chr.Guild == null)
            {
                menu.AddItem(new GossipMenuItem("You're not a part of a guild, if you were, this is where the guild master and delegates manage the guild kingdom infrastructure like defenses and economy."));
                chr.StartGossip(menu);
                return;
            }

            if (!KingdomGuildMgr.Guilds.ContainsKey(chr.Guild.Id))
            {
                menu.AddItem(new GossipMenuItem("Your guild has not yet participated in the Kingdom Wars"));
                chr.StartGossip(menu);
                return;
            }

            var gEx = KingdomGuildMgr.Guilds[chr.Guild.Id];

            if (chr.GuildRank > gEx.ReadAccess)
            {
                menu.AddItem(new GossipMenuItem("You are not allowed to access this function for your guild."));
                chr.StartGossip(menu);
                return;
            }

            bool writeAccess = false;

            if (chr.GuildRank <= gEx.WriteAccess)
            {
                writeAccess = true;
            }

            menu.AddItem(new GossipMenuItem("Welcome to the Kingdom Wars Menu"));

            GossipMenu territoryMenu = new GossipMenu();

            menu.AddItem(new GossipMenuItem("Manage Territories", territoryMenu));

            if (gEx.OwnedTerritories.Count == 0)
            {
                territoryMenu.AddItem(new GossipMenuItem("Your guild no longer control any territories."));
            }
            else
            {
                foreach (Territory t in gEx.OwnedTerritories)
                {
                    GossipMenu subTerritoryMenu = new GossipMenu(); 
                    territoryMenu.AddItem(new GossipMenuItem("Manage " + t.Template.Name, subTerritoryMenu));
                    
                    GossipMenu subMenuUpgrades = new GossipMenu();
                    GossipMenu subMenuStatus = new GossipMenu();
                    GossipMenu subMenuEnemyReport = new GossipMenu();

                    subTerritoryMenu.AddItem(new GossipMenuItem("Management of " + t.Template.Name));
                    subTerritoryMenu.AddItem(new GossipMenuItem("Upgrades", subMenuUpgrades));
                    subTerritoryMenu.AddItem(new GossipMenuItem("Local Status Report", subMenuStatus));
                    subTerritoryMenu.AddItem(new GossipMenuItem("Local Scout Report", subMenuEnemyReport));

                    // Handle Upgrades
                    subMenuUpgrades.AddItem(new GossipMenuItem("Upgrades available for " + t.Template.Name));

                    subMenuUpgrades.AddGoBackItem();
                    // End of Upgrades

                    // Handle Status
                    subMenuStatus.AddItem(new GossipMenuItem("Current status of " + t.Template.Name));
                    
                    subMenuStatus.AddGoBackItem();
                    // End of Status
                    
                    // Handle Enemy Report
                    subMenuEnemyReport.AddItem(new GossipMenuItem("Local scout report for " + t.Template.Name));

                    subMenuEnemyReport.AddGoBackItem();
                    // End of Enemy Report

                    subTerritoryMenu.AddGoBackItem();
                }
            }

            territoryMenu.AddGoBackItem();

            menu.AddItem(new GossipMenuItem("Settings"));

            chr.StartGossip(menu);
        }
    }
}
